package com.example.yibaodianzipinzheng.service;

import com.sun.jna.Native;
import com.sun.jna.platform.win32.BaseTSD;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.win32.StdCallLibrary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

//import javax.annotation.PostConstruct;

@Service
@Slf4j
public class WindowFocus {
//    public static final String WINDOW_NAME="Java(TM) Platform SE binary (32 位)";
    public static final String WINDOW_NAME="有道云笔记";
//    public static final String WINDOW_NAME="Foxmail";

//    @PostConstruct
//    public void targetWindowFocus(){
//        // 获取窗口句柄
//        log.info("targetWindowFocus is running!!!");
////        try {
////            Thread.sleep(1*1000l);
////        } catch (InterruptedException e) {
////            e.printStackTrace();
////        }
//        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, WINDOW_NAME);
//        if (hwnd != null) {
////            try {
////                Thread.sleep(1*1000l);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//            // 获取窗口大小
//            WinDef.RECT rect = new WinDef.RECT();
//            User32.INSTANCE.GetWindowRect(hwnd, rect);
//            int width = rect.right - rect.left;
//            int height = rect.bottom - rect.top;
//            log.info("窗口大小: " + width + "x" + height);
////            // 获取鼠标坐标
////            WinDef.POINT point = new WinDef.POINT();
//
//            User32.INSTANCE.ShowWindow(hwnd,3);
//            log.info("{}窗口前置！",WINDOW_NAME);
//            User32.INSTANCE.SetForegroundWindow(hwnd);
////            try {
////                Thread.sleep(1*1000l);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
//            log.info("{}窗口设置焦点！",WINDOW_NAME);
//            User32.INSTANCE.SetFocus(hwnd);
////            System.out.println("窗口坐标: (" + point.x + ", " + point.y + ")");
//        } else {
////            System.out.println("找不到窗口");
//            log.error("找不到窗口");
//        }
//    }

    public void mouseMoveAndClick(){
        this.moveMouseToPoint(100,100);
        this.mouseLeftClick();
    }

    public void moveMouseToPoint(int x, int y)
    {
        User32Mouse.INSTANCE.SetCursorPos(x,y);
    }
    public void mouseLeftClick()
    {
        User32Mouse.INSTANCE.mouse_event(User32Mouse.MOUSEEVENTF_LEFTDOWN, User32Mouse.DW_ZERO, User32Mouse.DW_ZERO, User32Mouse.DW_ZERO, User32Mouse.ULPTR_ZERO);
        User32Mouse.INSTANCE.mouse_event(User32Mouse.MOUSEEVENTF_LEFTUP, User32Mouse.DW_ZERO, User32Mouse.DW_ZERO, User32Mouse.DW_ZERO, User32Mouse.ULPTR_ZERO);
    }

    public interface User32Mouse extends StdCallLibrary {
        WindowFocus.User32Mouse INSTANCE = (WindowFocus.User32Mouse) Native.loadLibrary("user32", User32Mouse.class);
        //移动鼠标
        final int MOUSEEVENTF_MOVE = 0x0001;
        //模拟鼠标左键按下
        final WinDef.DWORD MOUSEEVENTF_LEFTDOWN = new WinDef.DWORD(0x0002L);
        //模拟鼠标左键抬起
        final WinDef.DWORD MOUSEEVENTF_LEFTUP = new WinDef.DWORD(0x0004L);
        //模拟鼠标右键按下
        final int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        //模拟鼠标右键抬起
        final int MOUSEEVENTF_RIGHTUP = 0x0010;
        //模拟鼠标中键按下
        final int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        //模拟鼠标中键抬起
        final int MOUSEEVENTF_MIDDLEUP = 0x0040;
        //标示是否采用绝对坐标
        final int MOUSEEVENTF_ABSOLUTE = 0x8000;
        //模拟鼠标滚轮滚动操作，必须配合dwData参数
        final int MOUSEEVENTF_WHEEL = 0x0800;

        final WinDef.DWORD DW_ZERO = new WinDef.DWORD(0L);

        final  BaseTSD.ULONG_PTR ULPTR_ZERO = new BaseTSD.ULONG_PTR();

/*
void mouse_event(
  [in] DWORD     dwFlags,
  [in] DWORD     dx,
  [in] DWORD     dy,
  [in] DWORD     dwData,
  [in] ULONG_PTR dwExtraInfo
        );

BOOL SetCursorPos(
  [in] int X,
  [in] int Y
);

BOOL ShowWindow(
  [in] HWND hWnd,
  [in] int  nCmdShow
);

        */

        void mouse_event(WinDef.DWORD dwFlags, WinDef.DWORD dx, WinDef.DWORD dy, WinDef.DWORD dwData, BaseTSD.ULONG_PTR dwExtraInfo);

        boolean SetCursorPos(int x, int y);

    }


    public static void main(String[] args) {
        WindowFocus windowFocus=new WindowFocus();
        windowFocus.mouseMoveAndClick();
    }
}
